class Node:
    def __init__(self, data=None):
        self.data = data
        self.next = None
        self.prev = None


class Fifo:
    def __init__(self, max_lengh):
        self.head = Node()
        self.tail = Node()
        self.max_lengh = max_lengh
        self.length = 0
        # 初始化 让两个关联起来
        self.head.next = self.tail
        self.tail.prev = self.head


# length 不包含 head 和 tail 把? count 节点的个数?
    def shift(self):
        assert not self.isempty()
        # tmp = self.head.next
        tmp = self.head.next

        # while tmp.next != self.tail:
        #     tmp = tmp.next


        if tmp.next is not None:
            del_curNode = tmp
            nextNode = tmp.next

            # tmp.next = del_curNode.next
            self.head.next = del_curNode.next
            # nextNode.prev = tmp
            nextNode.prev = self.head


            # self.head.next = nextNode
            # self.nextNode.prev = self.head

        self.length -= 1



    def unshift(self, data):
        # 设置一个游标 tmp
        assert not self.isfull()
        tmp = self.head
        while tmp.next != self.tail:
            tmp = tmp.next

        newNode = Node()
        newNode.data = data

        curNode = tmp
        nextNode =tmp.next

        # newNode = curNode.next
        curNode.next = newNode
        newNode.prev = curNode

        # nextNode.prev = newNode # nextNode 为空的时候 prev
        # newNode.next = nextNode
        newNode.next = nextNode
        if nextNode:
            nextNode.prev = newNode

        self.length += 1





    def isempty(self):
         return self.length == 0

    def isfull(self):
        return self.length == self.max_lengh

    def __len__(self):
        return self.length

    def __iter__(self):
        node = self.head.next
        # tmp = self.head.next
        while node != self.tail:
            yield node.data
            node = node.next



if __name__ == '__main__':
    l = Fifo(10)
    l.unshift(1)
    l.unshift(2)
    l.unshift(3)

    l.unshift(4)
    l.unshift(5)
    l.unshift(6)

    l.unshift(7)
    l.unshift(8)
    l.unshift(9)
    l.unshift(10)
    # l.unshift(11)
    l.shift()
    l.shift()
    for x in l:
        print(x)

















